package com.ego.sms.listener;

import com.ego.sms.utils.SendSms;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class SmsListener {

    @Resource
    private SendSms sendSms;


    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "ego.sms.exchange", ignoreDeclarationExceptions = "true"),
            value = @Queue(value = "ego.sms.queue", durable = "true"),
            key = {"sms.verify.code"})
    )
    public void sendSms(Map<String, String> map) {
        if (map == null || !(map.size() > 0)) {
            return;
        }
        String phone = map.get("phone");
        String code = map.get("code");
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(code)) {
            return;
        }
        sendSms.sendSms(phone, code);
    }

}
